import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 * 删除IDEA的试用记录，重新试用
 */
public class ResetIdea {
    public static void main(String[] args) {

        // 系统用户主目录路径
        final String userHome = System.getProperty("user.home");
        // 获取所有待重置的应用
        File appFile = new File(userHome + "/AppData/Local/JetBrains");
        List<String> ideaVersionList = new ArrayList<>();
        if (appFile.isDirectory()) {
            File[] list = appFile.listFiles();
            for (File f : list) {
                if (!"Toolbox".equals(f.getName())) {
                    ideaVersionList.add(f.getName());
                }
            }
        }

        // Toolbox管理安装的应用
        File toolFile = new File(userHome, "/AppData/Local/JetBrains/Toolbox/apps");
        if (toolFile.isDirectory()) {
            File[] list = toolFile.listFiles();
            for (File f : list) {
                if (!"Toolbox".equals(f.getName())) {
                    ideaVersionList.add(f.getName());
                }
            }
        }

        for (String ideaVersion : ideaVersionList) {

            // 文件目录路径，需要删除其下所有文件
            final String dirPath = userHome + "/AppData/Roaming/JetBrains/" + ideaVersion + "/eval";
            // 文件路径，需要删除它
            final String filePath = userHome + "/AppData/Roaming/JetBrains/" + ideaVersion + "/options/other.xml";

            File dir = new File(dirPath);
            File file = new File(filePath);
            if (dir.exists() && dir.isDirectory()) {
                final File[] eval = dir.listFiles();
                System.out.println("获取到文件夹文件：" + Arrays.toString(eval));
                assert eval != null;
                for (File cFile : eval) {
                    final boolean result = cFile.delete();
                    System.out.println("删除文件夹下文件：" + cFile.getName() + (result ? "（成功）" : "（失败）"));
                }

            }
            if (file.exists() && file.isFile()) {
                final boolean result = file.delete();
                System.out.println("删除文件夹下文件：" + file.getName() + (result ? "（成功）" : "（失败）"));
            }
            final Preferences preferences = Preferences.userRoot();
            final Preferences jetbrains = preferences.node("jetbrains");
            System.out.println("删除注册表" + jetbrains.name());
            try {
                jetbrains.removeNode();
            } catch (BackingStoreException e) {
                e.printStackTrace();
            }

        }

    }
}